00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef _string_utils_h_
00019 #define _string_utils_h_
00020
00021 #include <boost/algorithm/string.hpp>
00022 #include <vector>
00023
00024 namespace gridpack {
00025 namespace utility {
00026
00027
00028
00029
00030 class StringUtils {
00031 public:
00032
00033
00034
00035
00036 StringUtils() {};
00037
00038
00039
00040
00041 ~StringUtils() {};
00042
00043
00044
00045
00046
00047 void trim(std::string &str)
00048 {
00049
00050 int ntok;
00051 ntok = str.find('\n',0);
00052 while (ntok != std::string::npos) {
00053 str[ntok] = ' ';
00054 ntok++;
00055 ntok = str.find('\n',ntok);
00056 }
00057 ntok = str.find('\t',0);
00058 while (ntok != std::string::npos) {
00059 str[ntok] = ' ';
00060 ntok++;
00061 ntok = str.find('\t',ntok);
00062 }
00063 boost::trim(str);
00064 };
00065
00066
00067
00068
00069
00070 void toUpper(std::string &str)
00071 {
00072 boost::to_upper(str);
00073 }
00074
00075
00076
00077
00078
00079 void toLower(std::string &str)
00080 {
00081 boost::to_lower(str);
00082 }
00083
00084
00085
00086
00087
00088
00089
00090
00091 std::string clean2Char(std::string &string)
00092 {
00093 std::string tag = string;
00094
00095 int ntok1 = tag.find('\'',0);
00096 bool sngl_qt = true;
00097 bool no_qt = false;
00098
00099 if (ntok1 == std::string::npos) {
00100 ntok1 = tag.find('\"',0);
00101
00102 if (ntok1 == std::string::npos) {
00103 ntok1 = tag.find_first_not_of(' ',0);
00104 no_qt = true;
00105 } else {
00106 sngl_qt = false;
00107 }
00108 }
00109 int ntok2;
00110 if (sngl_qt) {
00111 ntok1 = tag.find_first_not_of('\'',ntok1);
00112 ntok2 = tag.find('\'',ntok1);
00113 } else if (no_qt) {
00114 ntok2 = tag.find(' ',ntok1);
00115 } else {
00116 ntok1 = tag.find_first_not_of('\"',ntok1);
00117 ntok2 = tag.find('\"',ntok1);
00118 }
00119 if (ntok2 == std::string::npos) ntok2 = tag.length();
00120 std::string clean_tag = tag.substr(ntok1,ntok2-ntok1);
00121
00122 ntok1 = clean_tag.find_first_not_of(' ',0);
00123 ntok2 = clean_tag.find(' ',ntok1);
00124 if (ntok2 == std::string::npos) ntok2 = clean_tag.length();
00125 tag = clean_tag.substr(ntok1,ntok2-ntok1);
00126 if (tag.length() == 1) {
00127 clean_tag = tag;
00128 clean_tag.append(" ");
00129 } else {
00130 clean_tag = tag;
00131 }
00132 return clean_tag;
00133 }
00134
00135
00136
00137
00138
00139
00140 std::string trimQuotes(std::string &str)
00141 {
00142 std::string tag = str;
00143
00144 int ntok1 = tag.find('\'',0);
00145 bool sngl_qt = true;
00146 bool no_qt = false;
00147
00148 if (ntok1 == std::string::npos) {
00149 ntok1 = tag.find('\"',0);
00150
00151 if (ntok1 == std::string::npos) {
00152 ntok1 = tag.find_first_not_of(' ',0);
00153 no_qt = true;
00154 } else {
00155 sngl_qt = false;
00156 }
00157 }
00158 int ntok2;
00159 if (sngl_qt) {
00160 ntok1 = tag.find_first_not_of('\'',ntok1);
00161 ntok2 = tag.find('\'',ntok1);
00162 } else if (no_qt) {
00163 ntok2 = tag.find_last_not_of(' ',ntok1)+1;
00164 } else {
00165 ntok1 = tag.find_first_not_of('\"',ntok1);
00166 ntok2 = tag.find('\"',ntok1);
00167 }
00168 if (ntok2 == std::string::npos) ntok2 = tag.length();
00169 std::string clean_tag = tag.substr(ntok1,ntok2-ntok1);
00170
00171 trim(clean_tag);
00172 return clean_tag;
00173 }
00174
00175
00176
00177
00178
00179
00180
00181 std::vector<std::string> blankTokenizer(std::string &str)
00182 {
00183
00184 std::string strcpy = str;
00185 int slen = strcpy.length();
00186 int i;
00187 for (i=0; i<slen; i++) {
00188 if (strcpy[i] == '\t') strcpy[i] = ' ';
00189 }
00190 int ntok1, ntok2;
00191 std::vector<std::string> ret;
00192 ntok1 = strcpy.find_first_not_of(' ',0);
00193 if (strcpy[ntok1] == '\'') {
00194 ntok2 = strcpy.find('\'',ntok1+1);
00195 ntok2++;
00196 } else if (strcpy[ntok1] == '\"') {
00197 ntok2 = strcpy.find('\"',ntok1+1);
00198 ntok2++;
00199 } else if (ntok1 != std::string::npos) {
00200 ntok2 = strcpy.find(' ',ntok1);
00201 if (ntok2 == std::string::npos) ntok2 = slen;
00202 } else {
00203 return ret;
00204 }
00205 ret.push_back(strcpy.substr(ntok1,ntok2-ntok1));
00206 while (ntok2 < slen-1 && ntok1 != std::string::npos) {
00207 ntok1 = strcpy.find_first_not_of(' ',ntok2);
00208 if (strcpy[ntok1] == '\'') {
00209 ntok2 = strcpy.find('\'',ntok1+1);
00210 if (ntok2 != std::string::npos) {
00211 ntok2++;
00212 } else {
00213 ntok2 = slen;
00214 }
00215 } else if (strcpy[ntok1] == '\"') {
00216 ntok2 = strcpy.find('\"',ntok1+1);
00217 if (ntok2 != std::string::npos) {
00218 ntok2++;
00219 } else {
00220 ntok2 = slen;
00221 }
00222 } else if (ntok1 != std::string::npos) {
00223 ntok2 = strcpy.find(' ',ntok1);
00224 if (ntok2 == std::string::npos) ntok2 = slen;
00225 }
00226 if (ntok2 != std::string::npos) {
00227 ret.push_back(strcpy.substr(ntok1,ntok2-ntok1));
00228 }
00229 }
00230 return ret;
00231 }
00232
00233
00234
00235
00236
00237
00238
00239
00240 std::vector<std::string> charTokenizer(std::string &str, const char *sep)
00241 {
00242 int slen = str.length();
00243 int ntok1, ntok2;
00244 std::vector<std::string> ret;
00245 ntok1 = str.find_first_not_of(sep,0);
00246 ntok2 = str.find(sep,ntok1);
00247 if (ntok2 == std::string::npos) ntok2 = slen;
00248 if (ntok1<=ntok2) ret.push_back(str.substr(ntok1,ntok2-ntok1));
00249 while (ntok2 < slen-1 && ntok1 != std::string::npos) {
00250 ntok1 = str.find_first_not_of(sep,ntok2);
00251 ntok2 = str.find(sep,ntok1);
00252 if (ntok2 == std::string::npos) ntok2 = slen;
00253 if (ntok1<=ntok2) ret.push_back(str.substr(ntok1,ntok2-ntok1));
00254 }
00255 return ret;
00256 }
00257
00258
00259
00260
00261
00262
00263
00264 bool getBool(const char* str) {
00265 std::string strcpy = str;
00266 trim(strcpy);
00267 toLower(strcpy);
00268 if (strcpy == "true") {
00269 return true;
00270 } else if (strcpy == "yes") {
00271 return true;
00272 } else if (strcpy == "t") {
00273 return true;
00274 } else if (strcpy == "y") {
00275 return true;
00276 } else if (strcpy == "1") {
00277 return true;
00278 } else if (strcpy == "false") {
00279 return false;
00280 } else if (strcpy == "no") {
00281 return false;
00282 } else if (strcpy == "f") {
00283 return false;
00284 } else if (strcpy == "n") {
00285 return false;
00286 } else if (strcpy == "0") {
00287 return false;
00288 }
00289 return false;
00290 }
00291 bool getBool(std::string str) {
00292 return getBool(str.c_str());
00293 }
00294
00295 };
00296
00297 }
00298 }
00299 #endif